var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();

Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0,
    postdata: {},
    isSubmit: false,
    expIntoDateStr: 15,
    value1: '',
    value2: 1,
    saleInfo: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    
    this.setData({
      expIntoDateStr: options.expIntoDateStr,
      value2: options.value2,
      dqyongjin: options.dqyongjin
    })
    network._post_json(this, api.getUserInfo, { appId: app.globalData.appId, openId: app.globalData.openId }, Osuccess);
    network._post_json(this, api.getSaleInfo, {}, saleInfoSuccess);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  inputamount: function (e) {
    var value = e.detail.value;
    if (contains(value, '.')) {
      var index = value.indexOf('.');
      var temp = value.substring(index, index.length);
      this.setData({
        value1: value.substring(0, index + 3)
      })
    } else {
      this.setData({
        value1: value
      })
    }
    this.data.postdata.amount = this.data.value1;
    isSubmit(this);
  },

  //提交数据
  formSubmit: function (e) {
    var reg = new RegExp("^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*)|([0]{1}))$");
    this.data.postdata.amount = e.detail.value.amount;
    if (e.detail.value.amount) {
      if (reg.test(this.data.postdata.amount)) {
        if (this.data.balance < (e.detail.value.amount * 100)) {
          this.showZanTopTips(2, '提现金额超出佣金总额');
        } else if (this.data.value2 > e.detail.value.amount * 100) {
          this.showZanTopTips((this.data.value2 / 100) + '元以上可提现');
        } else {
          network._post_json(this, api.applyWithdrawals, { amount: parseFloat(this.data.postdata.amount) * 100 }, success);
        }
      } else {
        this.showZanTopTips(2, '提现金额不合法');
      }
    } else {
      this.showZanTopTips(2, '请输入提现金额');
    }


  }

}))

var isSubmit = function (that) {
  that.setData({
    postdata: that.data.postdata
  })
  if (that.data.postdata.amount) {

    if (that.data.postdata.amount.length > 0) {
      that.setData({
        isSubmit: true
      })
    } else {
      that.setData({
        isSubmit: false
      })
    }


  } else {
    that.setData({
      isSubmit: false
    })
  }
}

var success = function (that, res) {
  if (res.data.data && res.data.data == '申请成功') {
    that.showZanToast('申请成功');
    setTimeout(function () {
      // wx.redirectTo({
      //   url: '/pages/myself/commission/shenqing',
      // })
      wx.navigateBack({

      })
    }, 1000)

  }
}

// 注水数据
var saleInfoSuccess = function (that, res) {
  if (res.data.data) {
    if (res.data.data.saleRule) {
      that.setData({
        saleInfo: res.data.data,
        notice: res.data.data.saleWaters
      })
    }
  }
}

var Osuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      balance: res.data.data.balance
    })

  }
}

var contains = function (str, s) {
  return str.indexOf(s) >= 0;
}